<template>
  <div class="app-container">
    <FixedBar>
      <el-button @click="onCancel">返回</el-button>
    </FixedBar>
<!--    <div style="text-align: right">-->
<!--      <el-button @click="onCancel">返回</el-button>-->
<!--    </div>-->
    <el-card class="box-card" style="margin-top: 20px">
      <div slot="header" class="clearfix">
        <span>基本信息</span>
      </div>
      <el-descriptions :column="4">
        <el-descriptions-item label="方案名称">{{info.programmeName}}</el-descriptions-item>
        <el-descriptions-item label="业务量分档（万件）">{{info.businessClassification}}</el-descriptions-item>
        <el-descriptions-item label="班次">{{info.timeShiftName}}</el-descriptions-item>
        <el-descriptions-item label="汇总人数">{{info.standardNumber}}</el-descriptions-item>
        <el-descriptions-item label="标准人效">{{info.standardEffect}}</el-descriptions-item>
        <el-descriptions-item label="方案状态">{{info.status === 1 ? '启用' : '禁用' }}</el-descriptions-item>
      </el-descriptions>
    </el-card>

    <el-card class="mt10 box-card">
      <div slot="header" class="clearfix">
        <span>人员配置</span>
      </div>
      <el-table
        class="staffTable"
        :data="staffData"
        :span-method="objectSpanMethod"
        border
        :summary-method="getSummaries"
        style="width: 100%">
        <el-table-column
          prop="regionName"
          label="区域"
          width="180">
          <template slot-scope="scope">
            <span :class="{ textDisable : scope.row.regionDisable }">{{scope.row.regionName}}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="groupName"
          label="组别">
          <template slot-scope="scope">
            <span :class="{ textDisable : scope.row.groupDisable }">{{scope.row.groupName}}</span>
          </template>
        </el-table-column>
<!--        <el-table-column-->
<!--          prop="groupNumber"-->
<!--          label="组别人数">-->
<!--        </el-table-column>-->
        <el-table-column
          prop="postName"
          label="岗位">
          <template slot-scope="scope">
            <span :class="{ textDisable : scope.row.postDisable }">{{scope.row.postName}}</span>
          </template>
        </el-table-column>
        <el-table-column
          width="150px"
          prop="postNumber"
          label="计划人数">
          <template slot-scope="scope">
            <span v-if="scope.row.postDisable" class="textDisable">— —</span>
            <span v-else>{{scope.row.postNumber}}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="quota"
          label="汇总人数">
          <template slot-scope="scope">
              <span :class="{ textDisable : scope.row.regionDisable }">
                计划总人数：{{scope.row.quota ? scope.row.quota : 0}}
              </span>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-card class="mt10 box-card">
      <div slot="header" class="clearfix">
        <span>设备配置</span>
      </div>
      <div class="device-wrap">
        <div style="margin-top: 10px">
          <span class="itemStatus">启用</span>
          <span class="itemStatus red">停用</span>
        </div>
        <el-table
          v-if="deviceData.length"
          :data="deviceData"
          border
          style="width: 100%;margin-top: 20px">
          <el-table-column
            prop="equipmentName"
            label="作业时间"
            width="180">
          </el-table-column>
          <el-table-column
            v-for="item in deviceTime"
            :prop="`${item}`"
            :label="`${item}`">
            <template slot-scope="scope">
              <div class="status" :class="{green:scope.row[item] == 1,red:scope.row[item] == 0}"></div>
            </template>
          </el-table-column>
        </el-table>
      </div>

    </el-card>
  </div>
</template>

<script>
  import { getSchemeDetail } from "@/api/job/scheme";
  import FixedBar from '@/components/FixedBar';
  export default {
    components:{
      FixedBar
    },
    data(){
      return {
        info: {},  //基本信息
        staffData: [], //人员配置数据
        deviceData: [], //设备配置数据
        deviceTime: [], //设备工作时间
      }
    },
    created(){
      const { id } = this.$route.query;
      if(id){
        this.getSchemeDetail(id)
      }
    },
    computed: {
      areaNum(){  //获取区域数组
        return new Set(this.staffData.map(o => o.regionName));
      },
      groupNum(){  //获取组别数组
        return new Set(this.staffData.map(o => o.groupName));
      }
    },
    methods: {
      onCancel() {
        this.$router.replace({path:"/basic/plan/scheme"});
      },
      getSchemeDetail(id){
        getSchemeDetail(id).then(res => {
          console.log(res)
          if(res.code === 200){
            const data = res.data || {};
            this.info = data.schemeConfiguration;
            this.staffData = data.deps;
            const deviceData = this.convertDeviceData(data.equipments)
            this.deviceData = deviceData;
          }
        })
      },
      //格式化设备数据
      convertDeviceData(data){
        const deviceData = []
        const deviceTime = [];
        const beginTime = data[0].beginTime;
        const endTime = data[0].endTime;
        data.forEach((item,index) => {
          if(beginTime>endTime){
            //晚班
            for(let i = beginTime,j=0; i< 24; i++,j++){
              item[i] = item.tableData[j];
              //获取设备时间
              if(index === 0){
                deviceTime.push(i)
              }
            }
            for(let i = 0,j=0; i< endTime; i++,j++){
              item[i] = item.tableData[j];
              //获取设备时间
              if(index === 0){
                deviceTime.push(i)
              }
            }
          }else {
            for(let i = beginTime,j=0; i<= endTime; i++,j++){
              item[i] = item.tableData[j];
              //获取设备时间
              if(index === 0){
                deviceTime.push(i)
              }
            }
          }
          deviceData.push(item)
        });
        this.deviceTime = deviceTime;
        return deviceData;
      },
      classGroup(name){  //根据组别名称获取组别数量
        return this.staffData.filter(o => o.groupName == name).length;
      },
      classArea(name){  //根据区域名称获取区域数量
        return this.staffData.filter(o => o.regionName == name).length;
      },
      classGroupLen(name){  //根据组别名称获取该区域第一个岗位的偏移位置
        const tmp = Array.from(this.groupNum);
        const index = tmp.indexOf(name);  //某岗位在全区域中的偏移位置
        let len = 0;
        for (let i = 0;i < index;i++){
          len += this.classGroup(tmp[i]);
        }
        return len;
      },
      classAreaLen(name){  //根据组别名称获取该区域第一个岗位的偏移位置
        const tmp = Array.from(this.areaNum);
        const index = tmp.indexOf(name);  //某岗位在全区域中的偏移位置
        let len = 0;
        for (let i = 0;i < index;i++){
          len += this.classArea(tmp[i]);
        }
        return len;
      },
      //获取汇总数据
      getSummaries(param) {
        const {columns, data} = param;
        const sums = [];
        columns.forEach((column, index) => {
          if (index === 0) {
            sums[index] = '汇总人数';
            return;
          }
          const needSum = ["groupNumber","quota"];
          if(needSum.indexOf(column.property) === -1){
            //不需要计算汇总
            return false
          }
          const values = data.map(item => Number(item[column.property]));
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return prev + curr;
              } else {
                return prev;
              }
            }, 0);
          } else {
            sums[index] = 'N/A';
          }
        });
        return sums;
      },
      objectSpanMethod({ row, column, rowIndex, columnIndex }) {
        // console.log(row,column,rowIndex,columnIndex)
        if (columnIndex === 0 || columnIndex === 4 ) {
          //判断区域rowspan数量
          const len = this.classArea(row.regionName);
          const lenName = this.classAreaLen(row.regionName);
          if (rowIndex === lenName) {   //某区域首位岗位
            return {
              rowspan:len,
              colspan:1
            }
          } else return {   //某区域非首位岗位
            rowspan: 0,
            colspan: 0
          };
        }else if(columnIndex === 1 ){
          //判断区域rowspan数量
          const len = this.classGroup(row.groupName);
          const lenName = this.classGroupLen(row.groupName);
          if (rowIndex === lenName) {   //某班级首位学生行
            return {
              rowspan:len,
              colspan:1
            }
          } else return {   //某班级非首位学生行
            rowspan: 0,
            colspan: 0
          };
        }
      },
    }
  }
</script>
<style>
  .device-wrap .el-table td{
    padding: 0;
  }
  .device-wrap .el-table td .cell{
    padding: 0;
  }
  .device-wrap .el-table td:first-child .cell{
    padding-left: 10px;
  }
</style>
<style scoped>
  .device-wrap .el-table td .status{
    min-height: 36px;
    box-sizing: border-box;
  }
  .device-wrap .el-table td .status.green{
    background-color: green;
  }
  .device-wrap .el-table td .status.red{
    background-color: red;
  }

  .device-wrap .itemStatus{
    padding-left: 40px;
    display: inline-block;
    line-height: 30px;
    position: relative;
    margin-right: 10px;
  }
  .device-wrap .itemStatus::before{
    content: "";
    display: inline-block;
    width: 35px;
    height: 20px;
    position: absolute;
    left: 0;
    top: 5px;
    background-color: green;
  }
  .device-wrap .itemStatus.red::before{
    background-color: red;
  }
</style>
